Imports System.Diagnostics
Imports System.IO
Imports System.Data.OleDb

Public Class PDF2TEXT

   Const TMPFILE As String = "~tmpfile"

   Private ProjectName As String
   Private PDF2TextCommand As String
   Private Parameters As String
   Private Conn As OleDbConnection



   Public Sub New(ByVal ConnectionString As String, ByVal ProjectName As String, ByVal PDF2TextCommand As String, ByVal Parameters As String)
      Me.ProjectName = ProjectName
      Me.PDF2TextCommand = PDF2TextCommand
      Me.Parameters = Parameters
      If InStr(Me.PDF2TextCommand, "\") = 0 Then Me.PDF2TextCommand = Environment.CurrentDirectory & "\" & Me.PDF2TextCommand

      ' Test the command
      Try
         Process.Start(PDF2TextCommand)
      Catch e As Exception
         Throw New Exception("PDF2TEXT unable to execute command '" & Me.PDF2TextCommand & "'. Error : " & e.Message)
      End Try

      ' Test the database
      Conn = New OleDbConnection
      Conn.ConnectionString = ConnectionString
      Conn.Open()

   End Sub

   Sub UpdateTextContent()

      Dim SQLString As String = "SELECT [id], [binary_content] FROM [page] WHERE [filename] Like '%.pdf' AND [text_content] IS NULL AND [binary_content] IS NOT NULL AND [project]=" & foms(ProjectName)
      Dim DA As New OleDbDataAdapter
      DA.SelectCommand = New OleDbCommand(SQLString, Conn)
      Dim RS As New System.Data.DataSet
      DA.Fill(RS)

      ' Loop through all new pages
      Dim myRow As System.Data.DataRow
      For Each myRow In RS.Tables(0).Rows
         WriteBinaryStream(TMPFILE & ".pdf", myRow("binary_content"))
         Dim TextContent As String = PDFFile2Text()
         Try
            File.Delete(TMPFILE & ".pdf")
            File.Delete(TMPFILE & ".txt")
         Catch
         End Try

         Dim Title As String
         If InStr(TextContent, vbNewLine) > 1 Then
            Title = Before(TextContent, vbNewLine)
            If Len(Title) > 255 Then Title = Left(Title, 255)
         Else
            Title = Left(TextContent, 255)
         End If

         If TextContent <> "" Then
            SQLString = "UPDATE [page] SET [title]=" & foms(Title) & ", [text_content]=" & foms(TextContent) & " WHERE [id]=" & myRow("id") & " AND [project]=" & foms(ProjectName)
            DBExecute(SQLString)
         End If
      Next

   End Sub

   Private Function PDFFile2Text() As String
      ' Get text from a pdf

      ' Delete text file
      Try
         File.Delete(TMPFILE & ".txt")
      Catch
      End Try

      ' Save pdf to text file
      Try
         ' Synchronous process without window
         Dim ConvertProcess As New Process
         ConvertProcess.StartInfo = New ProcessStartInfo(PDF2TextCommand, Me.Parameters & " " & TMPFILE & ".pdf")
         ConvertProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
         ConvertProcess.StartInfo.CreateNoWindow = True
         ConvertProcess.Start()
         ConvertProcess.WaitForExit()
      Catch
         Return ""
      End Try

      ' Load text
      Try
         Dim Reader As StreamReader = New StreamReader(TMPFILE & ".txt")
         Dim s As String = Reader.ReadToEnd()
         Reader.Close()
         Return s
      Catch
         Return ""
      End Try

   End Function

   Private Sub DBExecute(ByVal SQLString As String)
      Dim dbCmd As OleDbCommand = New OleDbCommand(SQLString, Conn)
      dbCmd.ExecuteNonQuery()
   End Sub
End Class
